package com.hnevc.myspeaker.Map;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.hnevc.myspeaker.R;
import com.hnevc.myspeaker.overlayutil.WalkingRouteOverlay;

public class MapActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    Button mSearchButton;
    MapView mapView;
    RoutePlanSearch mSearch;
    String speakeWords = null;
    SpeechSynthesizer speechSynthesizer;
    String AppId = "17225865";
    String APIKey = "0zCp5NHw6XiY2KCrUUVnmlvd";
    String secretKey = "h6hy3FUWU2IBvWcH3EGhCKEVeRvN7029";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        init();
    }
    private void init() {
        Log.d(TAG, "TestMessage: 1");
        mapView = findViewById(R.id.mapView);
        mSearch = RoutePlanSearch.newInstance();
        OnGetRoutePlanResultListener resultListener = new OnGetRoutePlanResultListener(){

            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

                WalkingRouteOverlay overlay = new WalkingRouteOverlay(mapView.getMap());
                Log.d(TAG, "onGetWalkingRouteResult: "+walkingRouteResult.getRouteLines());
                if (walkingRouteResult.getRouteLines().size() > 0) {
                    //获取路径规划数据,(以返回的第一条数据为例)
                    //为WalkingRouteOverlay实例设置路径数据
                    overlay.setData(walkingRouteResult.getRouteLines().get(0));
                    //在地图上绘制WalkingRouteOverlay
                    overlay.addToMap();
                }
                WalkingRouteLine walkingRouteLine = walkingRouteResult.getRouteLines().get(0);
                String startLocation = walkingRouteLine.getStarting().getTitle();
                //获取起始点位名称
                String endLocation = walkingRouteLine.getTerminal().getTitle();
                //获取终点点位名称
                int spendTime = walkingRouteLine.getDuration();
                //获取时间 秒
                int distance = walkingRouteLine.getDistance();
                //获取路长 米

                Log.d(TAG, "TestMessage: 2");
                Log.d(TAG, "onGetWalkingRouteResult: "+startLocation+endLocation+spendTime+"  "+distance);
                speakeWords = "从"+startLocation+"到"+endLocation+",路长"+distance+"米,耗时"+spendTime+"秒";
                initSpeak();


            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

            }
        };
        mSearchButton = findViewById(R.id.search);
        mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.d(TAG, "TestMessage: 3");
                if(speechSynthesizer != null){
                    speechSynthesizer.stop();
                }
                putData();

            }
        });
        Log.d(TAG, "TestMessage: 4");
        mSearch.setOnGetRoutePlanResultListener(resultListener);
    }

    private void putData() {
        Log.d(TAG, "TestMessage: 4");
        BaiduMap baiduMap = mapView.getMap();
        PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "西游记主题公园");
        PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "百度科技园");
        mSearch.walkingSearch((new WalkingRoutePlanOption())
                .from(stNode)
                .to(enNode));
        //116.399665,40.134459
        LatLng latLng = new LatLng(40.134459,116.399665);
        MapStatus mapStatus = new MapStatus.Builder()
                .target(latLng)
                .zoom(17)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        baiduMap.setMapStatus(mMapStatusUpdate);
    }

    private void initSpeak() {
        Log.d(TAG, "TestMessage: 5");
        speechSynthesizer = SpeechSynthesizer.getInstance();
        speechSynthesizer.setContext(this);
        speechSynthesizer.setAppId(AppId);
        speechSynthesizer.setApiKey(APIKey,secretKey);
        speechSynthesizer.auth(TtsMode.ONLINE);
        speechSynthesizer.initTts(TtsMode.ONLINE);
        speechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE,SpeechSynthesizer.MIX_MODE_DEFAULT);
        speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED,"5");
        speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER,"0");
        speechSynthesizer.speak(speakeWords);


        SpeechSynthesizerListener listener = new SpeechSynthesizerListener() {
            @Override
            public void onSynthesizeStart(String s) {
                Log.d(TAG, "onSynthesizeStart: "+s);
            }

            @Override
            public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {
                Log.d(TAG, "onSynthesizeDataArrived: "+s);
            }

            @Override
            public void onSynthesizeFinish(String s) {
                Log.d(TAG, "onSynthesizeFinish: "+s);
            }

            @Override
            public void onSpeechStart(String s) {
                Log.d(TAG, "onSpeechStart: "+s);
            }

            @Override
            public void onSpeechProgressChanged(String s, int i) {
                Log.d(TAG, "onSpeechProgressChanged: "+s);
            }

            @Override
            public void onSpeechFinish(String s) {
                Log.d(TAG, "onSpeechFinish: "+s);
            }

            @Override
            public void onError(String s, SpeechError speechError) {
                Log.d(TAG, "onError: "+s);
            }
        };

        speechSynthesizer.setSpeechSynthesizerListener(listener);
        Log.d(TAG, "TestMessage: 6");


    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
